//
//  LS_searchBar.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/20.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class LS_searchBar: UIView {

    @IBOutlet weak var rightCons: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var searchTF: UITextField!
   
    //提供加载xin的类方法
    class func loadSearchBar() ->LS_searchBar{
        let searchView = NSBundle.mainBundle().loadNibNamed("LS_searchBar", owner: nil, options: nil).last as!LS_searchBar
        
        return searchView
        
    }
    
    //在  awakeFromNib 做初始设置
    override func awakeFromNib() {
        self.bounds.size.width = ScreenWidth
        //文本输入框有leftView
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置显示模式
        searchTF.leftViewMode = .Always
        //修改frame
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        //设置视图的显示模式
        searchTF.leftView!.contentMode = .Center

        //设置圆角
                searchTF.layer.cornerRadius = 20
                searchTF.layer.masksToBounds = true
        //设置边线的颜色
               searchTF.layer.borderColor = UIColor.orangeColor().CGColor
                searchTF.layer.borderWidth = 2
    }
    @IBAction func textFiledBeginEditing(sender: AnyObject) {
        UIView.animateWithDuration(0.25) {
            
            //修改约束
            self.rightCons.constant = self.cancelBtn.bounds.width
            //提前强制刷新视图
            self.layoutIfNeeded()
        }
    }
    @IBAction func CancelBtnDidClick(sender: AnyObject) {
        //修改约束
        //收集约束的变化
        self.rightCons.constant = 0
        UIView.animateWithDuration(0.25) {
            //提前强制刷新视图
            //1.自动布局的视图 有frame 吗?  有frame
            //2.视图的frame 实在layoutSubviews方法中根据约束来设置
            //3.提前调用了layoutSubviews
            //更新视图的frame发生在 一个动画闭包中
            //执行父视图的layoutIfNeeded
            self.layoutIfNeeded()
        }
                //结束编辑状态
        //        searchTF.resignFirstResponder()
        self.endEditing(true)
    }
}
